/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
// asdfgasdgsag
// asdfasdf
/*
 * LogIn.java
 *
 * Created on Nov 15, 2009, 8:27:02 PM
 */

package DigiPtShared;


import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author OxyG3n
 */
public class LogIn extends javax.swing.JFrame {

    /**
     *
     *  variables
     *
     */

    private static Statement stmt;

    /** Creates new form LogIn */
    public LogIn() {
        initComponents();
        login_error.setVisible(false);
    }

    public String verifyString(String input){
        String temp = "";
        for(int i=0;i<input.length(); i++){
            char x = input.charAt(i);
            if((x==' ') || (x=='\n')){
                return temp;
            }
            temp = temp.concat(x+"");
        }
        return temp;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        username_login = new javax.swing.JTextField();
        password_login = new javax.swing.JTextField();
        login_btn = new javax.swing.JButton();
        login_error = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("DigiPt Login");

        username_login.setForeground(new java.awt.Color(102, 102, 102));
        username_login.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        username_login.setText("username");
        username_login.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                username_loginMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                username_loginMouseExited(evt);
            }
        });

        password_login.setForeground(new java.awt.Color(102, 102, 102));
        password_login.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        password_login.setText("password");
        password_login.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                password_loginMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                password_loginMouseExited(evt);
            }
        });

        login_btn.setText("Login");
        login_btn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                login_btnMouseClicked(evt);
            }
        });

        login_error.setForeground(new java.awt.Color(255, 0, 0));
        login_error.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        login_error.setText("sss");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(login_error, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(157, 157, 157)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(login_btn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                    .addComponent(password_login, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                    .addComponent(username_login, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
                .addGap(157, 157, 157))
            .addGroup(layout.createSequentialGroup()
                .addGap(155, 155, 155)
                .addComponent(jLabel1)
                .addContainerGap(159, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel1)
                .addGap(41, 41, 41)
                .addComponent(login_error)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(username_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(password_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(login_btn)
                .addContainerGap(98, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void username_loginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_username_loginMouseClicked
        if(username_login.getText().equals("username") == true){
            username_login.setText("");
        }
    }//GEN-LAST:event_username_loginMouseClicked

    private void username_loginMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_username_loginMouseExited
        if(username_login.getText().equals("")){
            username_login.setText("username");
        }
    }//GEN-LAST:event_username_loginMouseExited

    private void password_loginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_password_loginMouseClicked
        if(password_login.getText().equals("password") == true){
            password_login.setText("");
        }
    }//GEN-LAST:event_password_loginMouseClicked

    private void password_loginMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_password_loginMouseExited
        if(password_login.getText().equals("") == true){
            password_login.setText("password");
        }
    }//GEN-LAST:event_password_loginMouseExited

    private void login_btnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_login_btnMouseClicked
        try {
            ResultSet rset = stmt.executeQuery("SELECT password,id_funcionario,funcao FROM funcionario");

            /**
             *  1 coluna das passwords
             *  2 coluna dos logins
             *  3 coluna das funcoes
             */

            while(rset.next()){
                if((username_login.getText()).equals(rset.getString(2))){

                    String password_db = verifyString(rset.getString(1));
                    System.out.println("+"+password_db+"+");

                    if((password_login.getText()).equals(password_db)){
                        login_error.setText("welcome " + rset.getString(3));
                        login_error.setVisible(true);

                        String cargo = verifyString(rset.getString(3));
                        if(cargo.equals("gerente")){
                            Gerente manager = new Gerente();
                            manager.Gerente_view();
                            return;
                        }else{
                            Caixa worker = new Caixa();
                            worker.Caixa_view();
                            return;
                        }
                    }
                }
            }
            login_error.setText("invalid login");
            login_error.setVisible(true);
        } catch (SQLException ex) {
            
            /**
             *  Se for apanahado algum erro 
             *
             */
            System.out.println("erro na pesquisa");
            Logger.getLogger(LogIn.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_login_btnMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args []) throws SQLException {
        /**
         *
         * Ligaçao a base de dados
         *
         */

        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "digipt", "admin");
        stmt = conn.createStatement();


        /*
         *
         * run grafic interface
         *
         */

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LogIn().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton login_btn;
    private javax.swing.JLabel login_error;
    private javax.swing.JTextField password_login;
    private javax.swing.JTextField username_login;
    // End of variables declaration//GEN-END:variables



}
